package com.tiger.novelbld.base;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
/**
 * 数据接口
 * @author tiger.chenlin
 * @date:2011-5-8 下午01:14:49
 * @file:DataServer.java
 * @package:com.tiger.novelcrazyper
 */
public class DataServer {
	private Thread mThread;
	private Handler mThreadHandler = null;
	private DataServerFactory mServerData = null;
	private HashMap<String, IServerResponseListener>listenerList = null;
	private Context currentContext = null;
	private static DataServer _self = null;
	public interface IServerResponseListener
	{
		public void handleResponse(int reqId, boolean result);
	}
	public void addListener(String key, IServerResponseListener listener)
	{		
		if(key == null || listener == null)
		{
			return;
		}
		
		if(listenerList == null)
			listenerList = new HashMap<String, IServerResponseListener>();
		if(listenerList.containsKey(key))
		{
			listenerList.remove(key);
		}
		listenerList.put(key, listener);
	}
		

	public void removeListener(String key)
	{
		if (null == key || null == listenerList) {
			return;
		}		
		listenerList.remove(key);
		
		if(0 >= listenerList.size())
			listenerList = null;
	}
	private DataServer()
	{
		mServerData = DataServerFactory.getInstance();
		
		mThread = new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				Looper.prepare();
				if(mThreadHandler == null)
					mThreadHandler = new ServerHandler();				
				Looper.loop();
			}
		});
		
		mThread.start();
	}
	public static DataServer getInstance(Context c)
	{
		if(_self == null)
		{
			_self = new DataServer();			
		}
		_self.currentContext = c;
		return _self;
	}	
	private class ServerHandler extends Handler
	{
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Bundle b = msg.getData();
			Boolean result = false;
			switch(msg.what)
			{
			case CommonUtil.sendMessage.READ_TXT:
				result = handleReqGetMenu();
				break;
			default:
				break;
			}
			if(null != listenerList)
			{
				Iterator<String> iterator = listenerList.keySet().iterator();
				while(iterator.hasNext())
				{
					String key = iterator.next();
					IServerResponseListener listener = listenerList.get(key);
					if(listener != null){
						listener.handleResponse(b.getInt("reqId"), result);
					}
					else
						listenerList.remove(key);
				}
			}else{
				Log.i("ocn", "listenerList is null");
			}
			super.handleMessage(msg);
		}		
	}
	private void sendMessage(int what, Bundle data)
	{   
		while(mThreadHandler == null)
		{
			SystemClock.sleep(200);
		}
		
		Message msg = mThreadHandler.obtainMessage(what);
		if(data != null)
			msg.setData(data);
		mThreadHandler.sendMessage(msg);
	}
	private boolean handleReqGetMenu() {
		try{
//		mServerData.setInfos(CommonUtil.getAVmenu(currentContext));
		return true;
		}catch(Exception exp){
			exp.printStackTrace();
			return false;
		}
	}
	public void sendReqGetMenu(int reqId)
	{
		Bundle b = new Bundle();
		b.putInt("reqId", reqId);
		sendMessage(reqId, b);
	}
}
